using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using DTO;
using System.Configuration;
using System.Data;

namespace DAL
{
    public class NHANVIEN_DAL
    {
        private SqlConnection cn = null;
        public NHANVIEN_DAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"]
                 .ConnectionString);
            cn.Open();
          
        }
        public void insert(NhanVien_DTO nhanvien)
        {
            SqlCommand cmd = new SqlCommand("NHANVIEN_INSERT", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@TENNV",nhanvien.TenNV);
            cmd.Parameters.AddWithValue("@MATKHAU",nhanvien.MatKhau);
            cmd.Parameters.AddWithValue("@TAIKHOAN", nhanvien.TaiKhoan);
            cmd.Parameters.AddWithValue("@MAQUYEN", nhanvien.MaQuyen);
            cmd.Parameters.AddWithValue("@DIACHI", nhanvien.DiaChi);
            cmd.Parameters.AddWithValue("@DIENTHOAI", nhanvien.DienThoai);
            cmd.Parameters.AddWithValue("@EMAIL", nhanvien.Email);
            cmd.ExecuteNonQuery();
        }
        public void update(NhanVien_DTO nhanvien)
        {
            SqlCommand cmd = new SqlCommand("NHANVIEN_UPDATE", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@MANV", nhanvien.MaNV);
            cmd.Parameters.AddWithValue("@TENNV", nhanvien.TenNV);
            cmd.Parameters.AddWithValue("@MATKHAU", nhanvien.MatKhau);
            cmd.Parameters.AddWithValue("@TAIKHOAN", nhanvien.TaiKhoan);
            cmd.Parameters.AddWithValue("@MAQUYEN", nhanvien.MaQuyen);
            cmd.Parameters.AddWithValue("@DIACHI", nhanvien.DiaChi);
            cmd.Parameters.AddWithValue("@DIENTHOAI", nhanvien.DienThoai);
            cmd.Parameters.AddWithValue("@EMAIL", nhanvien.Email);
            cmd.ExecuteNonQuery();
        }
        public void delete(int manv)
        {
            SqlCommand cmd = new SqlCommand("NHANVIEN_DELETE", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@MANV", manv);
            cmd.ExecuteNonQuery();
        }
        public tbNhanVienCollection Get(String where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int TOTALRECORDS)
        {
            SqlCommand cm = new SqlCommand("NHANVIEN_GET", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);

            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbNhanVienCollection NVCollection = new tbNhanVienCollection();
            while (dr.Read ())
            {
                NhanVien_DTO nv = new NhanVien_DTO();
                nv.MaNV  = int.Parse(dr.GetValue(0).ToString());
                nv.TenNV = dr.GetValue(1).ToString();
                nv.MatKhau =dr.GetValue(2).ToString();
                nv.TaiKhoan = dr.GetValue(3).ToString();
                nv.MaQuyen = int.Parse (dr.GetValue(4).ToString());
                nv.DiaChi= dr.GetValue(5).ToString();
                nv.DienThoai = dr.GetValue(6).ToString();
                nv.Email = dr.GetValue(7).ToString();

                NVCollection.Add (nv );


            }
            dr.Close();
            TOTALRECORDS = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return NVCollection;


        }
     
    }
}
